/**
 * @class   wosh::communication::NotificationFlow
 * @brief   This class holds history of a wosh::Notification, mainly used while
 *          notifications are processed by CommunicationManager.
 *
 * @see     wosh::services::CommunicationManagerBundle
 ****************************************************************************
 * @version $Id: NotificationFlow.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Communication_NotificationFlow_H__
 #define __WOSH_Communication_NotificationFlow_H__

 #include <woshDefs.h>
 #include <core/Serialization.h>
 #include <core/Notification.h>


namespace wosh {
 namespace communication {

class NotificationFlow : public virtual ISerializable
 {
	WOSH_SERIALIZABLE(wosh::communication::NotificationFlow)
	WOSH_COMPARE_RTTI(wosh::communication::NotificationFlow)

	public:

		NotificationFlow( Notification* original_notification = NULL );

		NotificationFlow( const NotificationFlow& m );

		virtual ~NotificationFlow();

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		inline void setID( long value )						{ this->uid = value; }

		inline void setArchived( bool value )				{ this->archived = value; }

		void setProcessing( bool value, long epoch = 0 );
		inline void processing( int offset = 1 )			{ this->processed += offset; }

		void raiseError( int offset = 1 );

		void sending( int offset = 1 );

		void setOriginal( Notification* original_notification );

		inline void setSourceCommunicator( const URI& uri )	{ this->sourceCommunicator = uri; }
		inline void setSenderCommunicator( const URI& uri )	{ this->senderCommunicator = uri; }

		void addInterpreted( Notification* interpreted_notification );

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		inline long getID() const						{ return this->uid; }

		inline bool isArchived() const					{ return this->archived; }

		inline bool isProcessing() const				{ return (this->processingTs > 0); }

		inline long getProcessingTs() const				{ return this->processingTs; }
		inline long getProcessingCount() const			{ return this->processed; }

		inline long getErrorsCount() const				{ return this->errors; }
		inline long getErrorTs() const					{ return this->errorTs; }

		inline bool hasBeenSent() const					{ return (this->sentCounter > 0); }
		inline long getSentTs() const					{ return this->sentTs; }
		inline long getSentCount() const				{ return this->sentCounter; }

		const URI& getSourceCommunicator() const		{ return this->sourceCommunicator; }
		URI& getSourceCommunicator()					{ return this->sourceCommunicator; }

		const URI& getSenderCommunicator() const		{ return this->senderCommunicator; }
		URI& getSenderCommunicator()					{ return this->senderCommunicator; }

		inline bool hasOriginal() const					{ return (this->original != NULL); }

		inline const Notification* getOriginal() const	{ return this->original; }
		inline Notification* getOriginal()				{ return this->original; }

		const Notification* getFinal() const;
		Notification* getFinal();

		const std::vector<wosh::Notification*>& getInterpreted() const	{ return this->interpreted; }
		std::vector<wosh::Notification*>& getInterpreted()				{ return this->interpreted; }

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual NotificationFlow& operator=(const NotificationFlow& m);

		virtual bool operator==(const NotificationFlow &other) const;
		virtual bool operator!=(const NotificationFlow &other) const { return !(*this == other);  }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:
		void assign_ID();
		virtual void clear();

/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		long uid;			///< unique ID of the flow
		bool archived;		///< archived flag, means the flow has been finished

		long errors;		///< number of errors raised while processing
		long errorTs;		///< epoch of last error (used within timeout check)

		long sentCounter;	///< global counter of retries
		long sentTs;		///< epoch of last sending request (used within timeout check)

		long processingTs;	///< epoch of processing request (used within timeout check)

		long processed;		///< how many times flow has been processed

		wosh::Notification* original;				///< the original notification

		std::vector<wosh::Notification*> interpreted;	///< the interpreted/translated notifications

		URI sourceCommunicator;
		URI senderCommunicator;


//@}

}; // class def

 }; // namespace communication
}; // namespace wosh

#endif //__WOSH_Communication_NotificationFlow_H__
